package bookstudytest.socket;

import java.io.*;
import java.net.Socket;

/**
 * @Auther: Lee
 * @Date: 2018/6/21 15:44
 * @Description:
 */
public class SocketClientDemo {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream outputStream =null;
        InputStream inputStream = null;
        String s = "";
        try {
            //建立连接
            socket = new Socket("127.0.0.1",9553);
            //获得字节输出流
            outputStream = socket.getOutputStream();
            //字符输出流
            PrintWriter printWriter = new PrintWriter(outputStream);
            //发送数据
            printWriter.write("Hello server this is client!");
            printWriter.flush();
            //关闭客户端的输出流
            socket.shutdownOutput();
            //字节输入流
            inputStream = socket.getInputStream();
            //字符输入流
            InputStreamReader reader = new InputStreamReader(inputStream);
            //缓冲字符流
            BufferedReader br = new BufferedReader(reader);

            while ((s=br.readLine())!=null){
                System.out.println(s);
            }
            //关闭资源
            printWriter.close();
            outputStream.close();
            br.close();
            reader.close();
            inputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
